package org.bcnApps.domain;


import java.util.ArrayList;
import java.util.Random;

import org.bcnApps.activity.R;
import org.bcnApps.utils.GV;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;

public class Nuvols {
	
	private ArrayList<Objecte> nuvols;
	private final int MAX_NUVOLS = 8;
	private Random rand;
	Resources resources;
	BitmapFactory.Options options;
	
	public Nuvols() {
		nuvols = new ArrayList<Objecte>();
		rand = new Random();
		options = new BitmapFactory.Options();
		options.inPreferredConfig = Bitmap.Config.ARGB_8888;
		resources = GV.Instancies.resources;
	}
	
	public void actualitza() {
		toca_afegir();
		for(int i=nuvols.size()-1; i>=0; i--) {
			Objecte nuvol = nuvols.get(i);
			nuvol.actualitza();
			if((nuvol.vx > 0 && nuvol.x > GV.Screen.metrics.widthPixels) || (nuvol.vx < 0 && nuvol.x < -nuvol.tx)) nuvols.remove(i);
		}
	}
	
	public void draw(Canvas canvas) {
		for(Objecte o: nuvols) o.draw(canvas);
	}
	
	private void toca_afegir() {
		if(nuvols.size() == MAX_NUVOLS) return;
		for(int i=nuvols.size(); i < MAX_NUVOLS; i++) {
			Bitmap nuvol;
			if(rand.nextInt(2)==0) nuvol = BitmapFactory.decodeResource(resources, R.drawable.cloud1, options);
			else nuvol = BitmapFactory.decodeResource(resources, R.drawable.cloud2, options);
			float x,y,tx,ty,vx,vy;
			tx = (250f+(float)rand.nextInt(250))/1000*GV.Screen.metrics.widthPixels;
			ty = (125f +(float)rand.nextInt(150))/1000*GV.Screen.metrics.heightPixels;
			y = (float)rand.nextInt(GV.Screen.metrics.heightPixels-(int)ty);
			vx = (0.0015f+(float)rand.nextInt(40)/10000)*GV.Screen.metrics.widthPixels;
			vy = 0;
			if(rand.nextInt(2)==0) {
				x = -tx;
			}
			else {
				x = GV.Screen.metrics.widthPixels;
				vx = -vx;
			}
			nuvols.add(new Objecte(nuvol,x,y,tx,ty,vx,vy));
		}
	}
}
